//
//  Bullet.m
//  Project 5
//
//  Created by Nguyen Tan Loc on 6/18/13.
//  Copyright (c) 2013 Nguyen Tan Loc. All rights reserved.
//

#import "Bullet.h"
#import "PhysicBody.h"
#import "PhysicSystem.h"
#import "CircleSegment.h"

@implementation Bullet
@synthesize _colorType = mColorType, _type = mType;

-(id) initWithType:(BULLET_TYPE)type color:(COLOR_TYPE)color
{
    mType = type;
    mColorType = color;
    mRadius = 10;
    mIsCheckCollision = YES;
    mIsDestroy = NO;
    
    if (self = [super initWithSpriteFrameName:[self getSpriteFrameName]])
    {
        [self initRatio];
        [self initPhysicBody];
    }
    return self;
}

-(void) initPhysicBody
{
    PhysicBody *body = [[PhysicBody alloc] initWithDelegate:self shapeType:TYPE_CIRCLE value:mRadius];
    [[PhysicSystem sharedPhysicSystem] addBodyToSystem:body withType:TYPE_PHYSIC_OBJECT_BULLET];
    
    [body release];
}

-(void) initRatio
{
    float ratio = mRadius / (self.contentSize.width / 2);
    [self setScale:ratio];
}

-(NSString *) getSpriteFrameName
{
    NSString *frameName;
    switch (mType) {
        case TYPE_NORMAL:
            switch (mColorType) {
                case TYPE_COLOR_RED:
                    frameName = FRAME_NAME_CIRCLE_SEGMENT_COLOR_RED;
                    break;
                    
                case TYPE_COLOR_BLUE:
                    frameName = FRAME_NAME_CIRCLE_SEGMENT_COLOR_BLUE;
                    break;
                    
                case TYPE_COLOR_GREEN:
                    frameName = FRAME_NAME_CIRCLE_SEGMENT_COLOR_GREEN;
                    break;
                    
                case TYPE_COLOR_PURPLE:
                    frameName = FRAME_NAME_CIRCLE_SEGMENT_COLOR_PURPLE;
                    break;
                    
                case TYPE_COLOR_YELLOW:
                    frameName = FRAME_NAME_CIRCLE_SEGMENT_COLOR_YELLOW;
                    break;
                    
                default:
                    frameName = FRAME_NAME_CIRCLE_SEGMENT_COLOR_RED;
                    break;
            }
            break;
            
        case TYPE_POWER_1:
            frameName = FRAME_NAME_CIRCLE_SEGMENT_COLOR_BLUE;
            break;
            
        case TYPE_POWER_3:
            frameName = FRAME_NAME_CIRCLE_SEGMENT_COLOR_GREEN;
            break;
            
        case TYPE_POWER_FULL:
            frameName = FRAME_NAME_CIRCLE_SEGMENT_COLOR_PURPLE;
            break;
            
        case TYPE_LASER:
            frameName = FRAME_NAME_CIRCLE_SEGMENT_COLOR_YELLOW;
            break;
            
        default:
            frameName = FRAME_NAME_CIRCLE_SEGMENT_COLOR_RED;
            break;
    }
    
    return frameName;
}

-(void) fireWithDirection:(CGPoint)direction
{
    mIsDestroy = NO;
    mDirection = ccpMult(direction, SPEED_BULLET);
    [self scheduleUpdate];
}

-(void) update:(ccTime)delta
{
    CGSize winSize = [[CCDirector sharedDirector] winSize];
    CGPoint worldPosition = [self.parent convertToWorldSpace:self.position];
    if (worldPosition.x <= -SIZE_BOUNDING || worldPosition.x >= winSize.width + SIZE_BOUNDING
        || worldPosition.y <= -SIZE_BOUNDING || worldPosition.y >= winSize.height + SIZE_BOUNDING)
    {
        [self destroyBullet];
    }
    
    float posX = self.position.x + mDirection.x * delta;
    float posY = self.position.y + mDirection.y * delta;
    [self setPosition:ccp(posX, posY)];
}

-(void) runEffect:(EFFECT_TYPE)effect
{
    
}

-(void) destroyBullet
{
    // add effect here
    [self unscheduleUpdate];
    mIsDestroy = YES;
    self.visible = NO;
}

-(BOOL) isDestroy
{
    return mIsDestroy;
}

-(void) setType:(BULLET_TYPE)type color:(COLOR_TYPE)color
{
    mType = type;
    mColorType = color;
    [self setDisplayFrame:[[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:[self getSpriteFrameName]]];
}

-(void) beginContact:(id)otherContact
{
    if ([otherContact isKindOfClass:[CircleSegment class]])
    {
        [self destroyBullet];
    }
}

-(Boolean) isCheckCollision
{
    return mIsCheckCollision;
}

-(void) dealloc
{
    [super dealloc];
}

@end
